package org.wx.herostory.services.encoder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.wx.herostory.model.User;
import org.wx.herostory.msg.GameMsgProtocol;
import org.wx.herostory.services.util.Broadcaster;
import org.wx.herostory.services.util.UserManager;
import org.wx.herostory.services.util.mq.MqProducer;
import org.wx.herostory.services.util.mq.VictorMsg;

import java.util.Random;

/**
 * @program: my-java
 * @description: 用户攻击
 * @author: Wang.xun
 * @create: 2021-03-01 22:00
 **/
@Slf4j
public class UserAttkCmdHandler implements  ICmdHandler<GameMsgProtocol.UserAttkCmd>{
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserAttkCmd cmd) {
        if(null == ctx || null == cmd){
          return;
        }
        // 攻击者id
        Integer attkUserId = (Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        if(null == attkUserId){
          return;
        }
        // 攻击的目标userId
        int targetUserId = cmd.getTargetUserId();
        log.info("攻击者:{} 被攻击者:{}",attkUserId,targetUserId);
        if(-1 == targetUserId){
            // 没有攻击到任何人
            broadcastAttkResult(attkUserId,-1);
            return;
        }
        User targetUser = UserManager.getUserById(targetUserId);

        // 已经被干死了，就不要继续减血，广播了
        if(targetUser.getCurrHp()<=0){
            broadcastAttkResult(attkUserId,-1);
            return;
        }

        // 随机攻击点数
        int dmgPoint = new Random().nextInt(20);
        // 减少血量
        targetUser.setCurrHp(targetUser.getCurrHp() - dmgPoint);

        broadcastAttkResult(attkUserId, targetUserId);
        // 广播掉血结果
        broadcastSubtractHpResult(targetUserId,dmgPoint);
        if(targetUser.getCurrHp()<=0){
            // 血量没有了 广播死亡结果
            broadcastDieResult(targetUserId);
            // 向mq中插入战绩值
            MqProducer.sendMsg("herostory_victor",new VictorMsg(attkUserId,targetUserId));
        }

    }

    /**
     * 广播攻击结果
     * @param attkUserId
     * @param
     */
    static private void broadcastAttkResult(int attkUserId,int targetUserId){
        if(attkUserId <=0){
            return ;
        }
        GameMsgProtocol.UserAttkResult.Builder resultBuilder = GameMsgProtocol.UserAttkResult.newBuilder();
        resultBuilder.setAttkUserId(attkUserId);
        resultBuilder.setTargetUserId(targetUserId);
        // 广播攻击结果
        Broadcaster.broadcast(resultBuilder.build());

    }

    /**
     * 减血操作
     * @param targetUserId 目标userId
     * @param subtractHp 减少的血量
     */
    static private void broadcastSubtractHpResult(int targetUserId,int subtractHp){
        if(targetUserId<=0|| subtractHp<=0){
            return;
        }
        GameMsgProtocol.UserSubtractHpResult.Builder builder = GameMsgProtocol.UserSubtractHpResult.newBuilder();
        builder.setTargetUserId(targetUserId);
        builder.setSubtractHp(subtractHp);
        // 广播
        Broadcaster.broadcast(builder.build());
    }

    /**
     * 广播死亡
     * @param targetUserId 目标的userId
     */
    static private void broadcastDieResult(int targetUserId){
        if(targetUserId <= 0){
            return;
        }
        GameMsgProtocol.UserDieResult.Builder builder = GameMsgProtocol.UserDieResult.newBuilder();
        builder.setTargetUserId(targetUserId);
        Broadcaster.broadcast(builder.build());
    }
}
